<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 24/9/14
 * Time: 11:15 AM
 */

class Category_Admin_Create_Controller extends Admin_Controller{

    public $restful = true;

    public function __construct()
    {
        parent::__construct();
        //--Your Code --//
        $this->theme->set_theme("admin") ;
    }

    public function get_index($pid=0){

        $p = DB::table('categories')->where('id', '=', $pid)->first();

        if($p){
            $data['parent_name'] = $p->name;
            $data['parent_id'] = $p->id;
        }else{
            $data['parent_name'] = 'Root Category';
            $data['parent_id'] = 0;
        }

        //dd($data['parent_name']);
        $this->theme->title("Admin:Create Category");
        return $this->theme->render('category::admin.create',$data);
    }

    public function post_index($pid=0){

        $rules = array(
            'parent_id' => 'required|integer',
            'parent_name' => 'required',
            'name' => 'required|min:3|max:100',
        );






        $validator = Validator::make(Input::get(), $rules);
        if( $validator->fails() )
        {
            return Redirect::back()->with_input()->with_errors($validator)->with('message','Invalid Operation !');
        }
        else
        {
            $categories = new Categories;
            $categories->parent_id = Input::get('parent_id');
            $categories->name = Input::get('name');
            $categories->slug = (Input::get('slug') ? Str::slug(Input::get('slug')) : Str::slug(Input::get('name') ));
            $categories->description = Input::get('description');
            $categories->meta_title = Input::get('meta_title');
            $categories->meta_keyword = Input::get('meta_keyword');
            $categories->meta_description = Input::get('meta_description');
            $categories->save();
            return Redirect::to('admin/home/child/'.Input::get('parent_id'));
        }
    }

} 